//
//  PicsRoundView.m
//  NewChat
//
//  Created by songKX on 16/3/1.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "PicsRoundView.h"

#import "UIView+SDAutoLayout.h"

@implementation PicsRoundView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

//重写初始化方法
-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        [self addViews];
        
    }
    return self;
    
}

- (void)addViews{
    
    _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    
    _scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width*4, 0);
    
    
    [_scrollView setContentOffset:CGPointMake([UIScreen mainScreen].bounds.size.width, 0) animated:NO];
    
     _scrollView.pagingEnabled=YES;
    
    _scrollView.showsHorizontalScrollIndicator=NO;

    _scrollView.showsVerticalScrollIndicator = NO;
    
    _scrollView.backgroundColor = [UIColor blueColor];
    
    
    [self addSubview:_scrollView];
    
    _scrollView.sd_layout
    .topSpaceToView(self,0)
    .leftSpaceToView(self,0)
    .rightSpaceToView(self,0)
    .bottomSpaceToView(self,0);
    
    _leftImageView = [[UIImageView alloc] initWithFrame : CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0)];
    _leftImageView.backgroundColor = [UIColor greenColor];
    
    [_scrollView addSubview:_leftImageView];

    _leftImageView.sd_layout
    .bottomSpaceToView(_scrollView,0);
    
    _rightImageView=[[UIImageView alloc]initWithFrame:CGRectMake(2*[UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, 0)];
    
    _rightImageView.backgroundColor = [UIColor orangeColor];
    
    [_scrollView addSubview:_rightImageView];

    _rightImageView.sd_layout
    .bottomSpaceToView(_scrollView,0);
    
    _centerImageView = [[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, 0)];
    _centerImageView.backgroundColor = [UIColor yellowColor];
    
    [_scrollView addSubview:_centerImageView];
    
    _centerImageView.sd_layout
    .bottomSpaceToView(_scrollView,0);
    
    //初始化三个视图
    _leftImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"3.jpg"]];
    
    _centerImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"0.jpg"]];
    
    _rightImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"1.jpg"]];
    
    
    //初始化pageControl
    _pageControl=[[UIPageControl alloc]init];
    
    //将分页下标赋给定义的 int 属性
    _pageControl.currentPage = _currentImageIndex;

  
    
    //设置颜色
    _pageControl.pageIndicatorTintColor=[UIColor blackColor];
    
    //设置当前页颜色
    _pageControl.currentPageIndicatorTintColor=[UIColor whiteColor];
    
    //设置总页数
    _pageControl.numberOfPages=4;
    
    [self addSubview:_pageControl];
    
    _pageControl.sd_layout
    .bottomSpaceToView(self,20)
    .heightIs(20)
    .leftSpaceToView(self,0)
    .rightSpaceToView(self,0);
    
    
    //添加scrollview代理
    self.scrollView.delegate = self;
    
    [self addTimer];
    
    
}
//添加定时器
-(void)addTimer{
    
    
    self.timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}
//取消定时器
-(void)cancelTimer{
    
    [self.timer invalidate];
    self.timer = nil;
    
}

//定时器方法
-(void)nextPage{
    
    CGPoint offset1=[self.scrollView contentOffset];
    
    offset1.x = [UIScreen mainScreen].bounds.size.width*2;
    
    [UIView animateWithDuration:0.5f animations:^{
        self.scrollView.contentOffset = offset1;
    }];
    
    self.currentImageIndex=(self.currentImageIndex+1)%4;
    
    [self sureImageLocation];
    
}


//图片位置重设

-(void)sureImageLocation{
    
    //定义左右图片的标号
    int leftImageIndex,rightImageIndex;
    
    self.centerImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"%i.jpg",self.currentImageIndex]];
    
    //重新设置左右图片
    
    leftImageIndex = (self.currentImageIndex+4-1)%4;
    
    rightImageIndex = (self.currentImageIndex+1)%4;
    
    self.leftImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"%i.jpg",leftImageIndex]];
    
    self.rightImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"%i.jpg",rightImageIndex]];
    
    //移动到中间
    [self.scrollView setContentOffset:CGPointMake([UIScreen mainScreen].bounds.size.width, 0) animated:NO];
    
    //设置分页
    self.pageControl.currentPage=self.currentImageIndex;
    
}


-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [self.timer invalidate];
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    //重新加载图片
    [self reloadImage];
    
    
    
}

-(void)reloadImage{
    
    
    CGPoint offset2=[self.scrollView contentOffset];
    
    if (offset2.x > [UIScreen mainScreen].bounds.size.width) { //向右滑动
        
        self.currentImageIndex=(self.currentImageIndex+1)%4;
        
    }else if(offset2.x < [UIScreen mainScreen].bounds.size.width){ //向左滑动
        self.currentImageIndex=(self.currentImageIndex+4-1)%4;
    }
    
    self.pageControl.currentPage=self.currentImageIndex;

    [self sureImageLocation];
    
    [self addTimer];
    
}


@end
